package com.qf.service.impl;

import com.qf.dao.TestDao;
import com.qf.mapper.UserMapper;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Controller
@Transactional(
        propagation = Propagation.SUPPORTS, // 传播行为
        timeout = -1, //超时时间
        rollbackFor = Exception.class,// 异常
        readOnly = true//是否只读事务
)
public class UserServiceImpl implements UserService {
//    @Autowired
//            @Qualifier("testDaoImpl")
    @Resource
    TestDao testDao;
    @Autowired
    @Qualifier("userMapper")
    UserMapper userMapper;


    public String test(int i) {
        return "金宇亭";
    }


    @Override

    public void transfer(String from, String to, int money) {

        //查询两个账户
        User fromUser = userMapper.findByName(from);
        User toUser = userMapper.findByName(to);

        //判断源 账户余额 - money 是否大于 0
        if(fromUser.getMoney() - money < 0) {
            System.out.println("刘润达哭了");
            return;
        }
        //转账
        fromUser.setMoney(fromUser.getMoney() - money);
        toUser.setMoney(toUser.getMoney() + money);
        //更新数据库
        userMapper.update(fromUser);
//        System.out.println(1/0);
        userMapper.update(toUser);

    }
}
